package com.liugh.service.zhongche.Imp;


import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liugh.entity.standard.StandardItem;
import com.liugh.mapper.zhongche.ESSearchMapper;
import com.liugh.mapper.zhongche.StandardItemMapper;
import com.liugh.model.ESModel;
import com.liugh.service.zhongche.IESModelService;
import com.liugh.util.CommonUtil;
import com.liugh.util.PDFUtil;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Objects;

@Service
public class ESModelServiceImp implements IESModelService {

    private static org.slf4j.Logger Logger = LoggerFactory.getLogger(ESModelServiceImp.class);


    private String PDF_READ_TEMPDIR;

    @Value("${PDF_READ_TEMPDIR}")
    public void setTEMPDIR(String str) {
        this.PDF_READ_TEMPDIR = str;
    }

    @Autowired
    StandardItemMapper itemMapper;

    @Autowired
    ESSearchMapper searchMapper;

    /**
     * ES添加索引
     * @param id
     */
    @Override
    public void addESModel(int id) throws URISyntaxException, IOException {
        StandardItem item = itemMapper.selectById(id);
        ESModel model = new ESModel();
        model.setId(id);
        model.setTitle(item.getStandName());
        model.setContent(PDFUtil.pdfToStringUrl(item.getUploadDir(),PDF_READ_TEMPDIR));
        model.setAuthor(item.getAuthor());
        searchMapper.save(model);
        Logger.info("已加入ES索引");
    }


    /**
     * 通过ES进行全文检索
     * @param page
     * @param key
     * @return
     * @throws JsonProcessingException
     */
    @Override
    public Page<StandardItem> fullTextQuery(Page<StandardItem> page, String key) throws JsonProcessingException {
        QueryStringQueryBuilder builder = new QueryStringQueryBuilder(key);
        Iterable<ESModel> searchResult = searchMapper.search(builder);
        Iterator<ESModel> iterator = searchResult.iterator();
        LinkedList<StandardItem> standardItems  = new LinkedList<>();
        while (iterator.hasNext()) {
            ESModel article = iterator.next();
            //格式化为json输出
            String jsonStr = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(article);
            StandardItem item = itemMapper.selectById(Objects.requireNonNull(CommonUtil.fromJson(jsonStr, StandardItem.class)).getId());
            if (item != null) {
                standardItems.add(item);
            }
        }
        page.setCurrent(page.getCurrent());
        page.setSize(page.getSize());
        page.setTotal(standardItems.size());
        page.setRecords(standardItems);
        return page;
    }
}
